package com.example.tallybook.ui.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.tallybook.data.Transaction
import com.example.tallybook.data.TransactionRepository
import com.example.tallybook.data.TransactionType
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import java.time.LocalDateTime
import javax.inject.Inject

@HiltViewModel
class TransactionViewModel @Inject constructor(
    private val repository: TransactionRepository
) : ViewModel() {

    private val _uiState = MutableStateFlow(TransactionUiState())
    val uiState: StateFlow<TransactionUiState> = _uiState.asStateFlow()

    init {
        loadTransactions()
        loadTotals()
    }

    private fun loadTransactions() {
        viewModelScope.launch {
            repository.getAllTransactions()
                .collect { transactions ->
                    _uiState.update { it.copy(transactions = transactions) }
                }
        }
    }

    private fun loadTotals() {
        viewModelScope.launch {
            combine(
                repository.getTotalIncome(),
                repository.getTotalExpense()
            ) { income, expense ->
                val totalIncome = income ?: 0.0
                val totalExpense = expense ?: 0.0
                val balance = totalIncome - totalExpense
                Triple(totalIncome, totalExpense, balance)
            }.collect { (income, expense, balance) ->
                _uiState.update { 
                    it.copy(
                        totalIncome = income,
                        totalExpense = expense,
                        balance = balance
                    )
                }
            }
        }
    }

    fun addTransaction(
        amount: Double,
        description: String,
        category: String,
        type: TransactionType,
        note: String = ""
    ) {
        if (amount <= 0) return
        
        val transaction = Transaction(
            amount = amount,
            description = description,
            category = category,
            type = type,
            note = note
        )
        
        viewModelScope.launch {
            repository.insertTransaction(transaction)
        }
    }

    fun deleteTransaction(transaction: Transaction) {
        viewModelScope.launch {
            repository.deleteTransaction(transaction)
        }
    }

    fun searchTransactions(query: String) {
        if (query.isBlank()) {
            loadTransactions()
            return
        }
        
        viewModelScope.launch {
            repository.searchTransactions(query)
                .collect { transactions ->
                    _uiState.update { it.copy(transactions = transactions) }
                }
        }
    }

    fun filterByType(type: TransactionType?) {
        if (type == null) {
            loadTransactions()
            return
        }
        
        viewModelScope.launch {
            repository.getTransactionsByType(type)
                .collect { transactions ->
                    _uiState.update { it.copy(transactions = transactions) }
                }
        }
    }

    fun insertSampleData() {
        viewModelScope.launch {
            repository.insertSampleData()
        }
    }
}

data class TransactionUiState(
    val transactions: List<Transaction> = emptyList(),
    val totalIncome: Double = 0.0,
    val totalExpense: Double = 0.0,
    val balance: Double = 0.0,
    val isLoading: Boolean = false,
    val error: String? = null
) 